<?php

import("apps.AbstractApp");

class FileRepositoryApp extends AbstractApp {
	
	var $CT_FILELIST	= "list";
	var $CT_UPLOAD 		= "upload";
	
	var $uploadForm;
	var $pageLocation;
	var $errors = array();
	var $resultProccessRequest = false;
	var $receiver = "igor.s.melnik@gmail.com";
	
	var $content_type = null;
	var $page_title = "";
	var $file_list = null;
	
	public function __construct() {
		import('net.html.form.Form');
		import('net.html.form.check.Validator');
		
		$this->uploadForm = new Form('uploadForm');
	}
	
	function setContentType($type) {
		return $this->content_type = $type;
	}

	function setPageLocation($page) {
		$this->pageLocation = $page;
	}
	function getPageLocation() {
		return $this->pageLocation;
	}
	
	function proccessRequest($request) {
		
		if (!$request) throw new Exception('$request not found in proccessRequest method');
		
		switch ($this->content_type) {
			case $this->CT_FILELIST:
				$this->page_title = "File List";

				import("data.logic.file.FileInfo");
				$this->file_list = FileInfo::findAll();
				break;
				
			case "upload":
				$this->page_title = "File Upload";
				
				import("data.logic.catalogue.CatalogueFactory");
				import("data.logic.lingvo.Bundle");
				$catalogue_id = "IMGR";
				$catalogue = CatalogueFactory::findCatalogue($catalogue_id);// createRemoteCatalogue("http://vh7/ws/catalogue/", $catalogue_id);
				try {
					$root = $catalogue->getRootNode();
					$node_list = $root->getChildren();
				} catch (Exception $e) {
					$node_list = array();
				}

				$categories = array();
				foreach ($node_list as $o) $categories[$o->getName()] = $o->getCaption();
				asort($categories);
				
				$cl_category  = $request->getParameter('category');
				$cl_description  = $request->getParameter('description');
				
				$this->uploadForm->addInput(array("type"=>"upload","name"=>"file","maxsize"=>2000000));
				//$this->uploadForm->addInput(array("type"=>"textfield","name"=>"name","style"=>"width: 450","value"=>"" ));
				//$form->addInput(array("type"=>"textfield","name"=>"email","style"=>"width: 450;","value"=>"" ));
				$this->uploadForm->addInput(array("type"=>"combobox","name"=>"category", "style"=>"width: 250", "value"=>$cl_category,
					"data"=>$categories, "empty"=>array(""=>"root")
				));
				$this->uploadForm->addInput(array("type"=>"textarea","name"=>"description","style"=>"width: 450; height: 160;","value"=>@$cl_description));
				$this->uploadForm->addInput(array("type"=>"submit","name"=>"btupload","value"=>"Upload"));
				
				if ($request->getMethod()=="POST") {
					/*import("net.html.form.check.Validator");
					$validator = new Validator($cl_name);
					$validator->validateSpaceString();
					$validator->validateEmptyString();
					if (!$validator->isValid()) {
						$this->addError("name","Incorrect data in field 'Name'");
					}*/
					
					import("util.FileUtils");
					$uploadFilename = FileUtils::toAnsiFileName($_FILES['file']['name'], true);
					$uploadFile = UPLOAD_PATH . DIRSEP . $uploadFilename;
		
					if (!move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
						$this->addError("upload","Upload error! Possible file upload attack!");
					}
			
					if (!$this->hasErrors()) {
						
						rename($uploadFile, REPOSITORY_PATH . DIR_SEP . $uploadFilename);
						
						import("data.logic.file.FileInfo");
						$fi = new FileInfo();
						
						$fi->setRealName(FileUtils::toAnsiFileName($_FILES['file']['name']));
						$fi->setRepName($uploadFilename);
						$fi->setDescription("");
						$fi->setSize($_FILES['file']['size']);
						$fi->setVersion("0");
						$fi->setStatus("new");
						$fi->setCategoryId($cl_category);
						$fi->store();
						
				    	$this->resultProccessRequest = true;
					} else {
						$this->resultProccessRequest = false;
						
					}
				}
				break;
				
			default:
				$this->page_title = "File Repository";
		}
	}
	
	function hasErrors() {
		return count($this->errors);
	}
	
	function addError($key, $message) {
		$this->errors[$key] = $message;
	}
	
	function getErrors() {
		return $this->errors;
	}
	
	function assignToTemplates(&$templates) {
		//Smarty
		$templates->assign("forms",array($this->uploadForm->getName()=>$this->uploadForm->toArray()) );
		$templates->assign_by_ref("controller", $this);
		$templates->assign("title_page", $this->page_title);
		$templates->assign("content_type", $this->content_type);
		$templates->assign("file_list", $this->file_list);

		if ($this->resultProccessRequest) {
			$templates->assign("result",array($this->uploadForm->getName()=>array("message"=>"Your message has been sent")) );
		}
	}
}
?>